package com.zbl.zblproject.core.controller;

import com.alipay.demo.trade.utils.GoodDateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbl.zblproject.core.entity.Pv;
import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.mapper.PvMapper;
import com.zbl.zblproject.core.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2019/7/12
 */
@Controller
@ResponseBody
public class PvController {
    @Resource
    private PvMapper pvMapper;

    @PostMapping("/insert")
    public Result insert(@RequestParam("ip") String ip,@RequestParam("city") String city,@RequestParam("page") String page){
        Pv pv = new Pv();
        long time = System.currentTimeMillis()/1000;
        pv.setIp(ip);
        pv.setCity(city);
        pv.setPage(page);
        pv.setCreateTime(time);
        int rs = pvMapper.insert(pv);
        if (rs>0){
            return ResultUtil.success("成功");
        }else{
            return ResultUtil.error("失败");
        }
    }

    @GetMapping("/getPvList")
    public Result  getPvList(){
        QueryWrapper<Pv> pvQueryWrapper = new QueryWrapper<>();
        pvQueryWrapper.orderByDesc("id");
        List<Pv> pvs=pvMapper.list();
        for (Pv pv:pvs){
            if (pv.getCreateTime()!=0){
                pv.setCreateTimes(GoodDateUtil.stampToDate(String.valueOf(pv.getCreateTime()*1000)));
            }else{
                pv.setCreateTimes("");
            }
        }
        return ResultUtil.success("成功",pvs);
    }
}
